﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Workflow.Common.Tools;
using Workflow.IDAO;
using Workflow.IService;
using Workflow.Model;
using Workflow.Model.Enum;

namespace Workflow.Service
{
	public class DistributeService : IDistributeService
	{
		public ICaseEEMDao CaseEEMDao { get; set; }//no log data
		public IWorkStatusDao WorkStatusDao { get; set; }
		public ICaseEEMService CaseEEMService { get; set; }//make log data
		
		
		#region eem
		public IList<string> UpdateEEMCaseForDistribute(IList<CaseEEM> cases, User user, User statusOperator)
		{
			var failCase = new List<string>();

			DateTime dateTime = ToolsHelper.GetTimeZoneNow();
			var distributedStatus = WorkStatusDao.Get(((int)EnumWorkStatus.Distributed).ToString());
			foreach (var oneCase in cases)
			{
				if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Distributed).ToString()
					|| oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Received).ToString())
				{
					oneCase.Operator = user;
					oneCase.StatusDate = dateTime;
					oneCase.WorkStatus = distributedStatus;
					oneCase.StatusOperator = statusOperator;
					//oneCase.CommentCode = null;
					CaseEEMService.SaveOrUpdateCase(oneCase);
				}
				else if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.WaitForCheck).ToString())
				{
					oneCase.Operator = user;
					oneCase.StatusDate = dateTime;
					//oneCase.CommentCode = null;
					//oneCase.StatusOperator = statusOperator;
					CaseEEMDao.SaveOrUpdate(oneCase);
				}
				else if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Reject).ToString())
				{
					failCase.Add(oneCase.CaseNo.ToString());
				}
				else
				{
					failCase.Add(oneCase.CaseNo.ToString());
				}
			}
			return failCase;
		}

		/// <summary>
		/// 将CASE列表分配给指定用户
		/// </summary>
		/// <param name="caseNos"></param>
		/// <param name="user"></param>
		public IList<string> UpdateEEMCaseForDistribute(IList<string> caseNos, User user, User statusOperator)
		{
			var failCase = new List<string>();

			DateTime dateTime = ToolsHelper.GetTimeZoneNow();
			var distributedStatus = WorkStatusDao.Get(((int)EnumWorkStatus.Distributed).ToString());

			foreach (var caseNo in caseNos)
			{
				var oneCase = CaseEEMDao.Get(Int64.Parse(caseNo));
				if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Distributed).ToString()
					|| oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Received).ToString())
				{
					oneCase.Operator = user;
					oneCase.StatusDate = dateTime;
					oneCase.WorkStatus = distributedStatus;
					//oneCase.CommentCode = null;
					oneCase.StatusOperator = statusOperator;
					CaseEEMService.SaveOrUpdateCase(oneCase);
				}
				else if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.WaitForCheck).ToString())
				{
					oneCase.Operator = user;
					oneCase.StatusDate = dateTime;
					//oneCase.CommentCode = null;
					//oneCase.StatusOperator = statusOperator;
					CaseEEMDao.SaveOrUpdate(oneCase);
				}
				else if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Reject).ToString())
				{
					failCase.Add(oneCase.CaseNo.ToString());
				}
				else
				{
					failCase.Add(oneCase.CaseNo.ToString());
				}
			}
			return failCase;
		}

		public IList<string> UpdateEEMCaseForPickUp(IList<CaseEEM> cases, User user, User statusOperator)
		{
			var failCase = new List<string>();

			DateTime dateTime = ToolsHelper.GetTimeZoneNow();
			var distributedStatus = WorkStatusDao.Get(((int)EnumWorkStatus.Distributed).ToString());
			foreach (var oneCase in cases)
			{
				if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Received).ToString())
				{
					oneCase.Operator = user;
					oneCase.StatusDate = dateTime;
					oneCase.WorkStatus = distributedStatus;
					oneCase.StatusOperator = statusOperator;
					//oneCase.CommentCode = null;
					CaseEEMService.SaveOrUpdateCase(oneCase);
				}
				else if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Reject).ToString())
				{
					failCase.Add(oneCase.CaseNo.ToString());
				}
				else
				{
					if (oneCase.Operator == null)//when wait for check && opterator is null then go
					{
						if (oneCase.StatusOperator != user)
						{
							oneCase.Operator = user;
							oneCase.StatusDate = dateTime;
							//oneCase.CommentCode = null;
							//oneCase.StatusOperator = statusOperator;
							CaseEEMDao.SaveOrUpdate(oneCase);
						}
						else
						{
							failCase.Add(oneCase.CaseNo.ToString());
						}
					}
					else
					{
						failCase.Add(oneCase.CaseNo.ToString());
					}
				}
			}
			return failCase;
		}

		/// <summary>
		/// 将CASE列表分配给指定用户
		/// </summary>
		/// <param name="caseNos"></param>
		/// <param name="user"></param>
		public IList<string> UpdateEEMCaseForPickUp(IList<string> caseNos, User user, User statusOperator)
		{
			var failCase = new List<string>();

			DateTime dateTime = ToolsHelper.GetTimeZoneNow();
			var distributedStatus = WorkStatusDao.Get(((int)EnumWorkStatus.Distributed).ToString());

			foreach (var caseNo in caseNos)
			{
				var oneCase = CaseEEMDao.Get(Int64.Parse(caseNo));
				if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Received).ToString())
				{
					oneCase.Operator = user;
					oneCase.StatusDate = dateTime;
					oneCase.WorkStatus = distributedStatus;
					//oneCase.CommentCode = null;
					oneCase.StatusOperator = statusOperator;
					CaseEEMService.SaveOrUpdateCase(oneCase);
				}
				else if (oneCase.WorkStatus.WorkStatusCode == ((int)EnumWorkStatus.Reject).ToString())
				{
					failCase.Add(oneCase.CaseNo.ToString());
				}
				else
				{
					if (oneCase.Operator == null)//when wait for check && opterator is null then go
					{
						if (oneCase.StatusOperator != user)
						{
							oneCase.Operator = user;
							oneCase.StatusDate = dateTime;
							//oneCase.CommentCode = null;
							//oneCase.StatusOperator = statusOperator;
							CaseEEMDao.SaveOrUpdate(oneCase);
						}
						else
						{
							failCase.Add(oneCase.CaseNo.ToString());
						}
					}
					else
					{
						failCase.Add(oneCase.CaseNo.ToString());
					}
				}
			}
			return failCase;
		}
		#endregion 

	}
}
